package org.ljy.lotterysystem.controller;

import org.ljy.lotterysystem.common.pojo.CommonResult;
import org.ljy.lotterysystem.common.utils.JacksonUtil;
import org.ljy.lotterysystem.controller.param.DrawPrizeParam;
import org.ljy.lotterysystem.controller.param.ShowWinningRecordsParam;
import org.ljy.lotterysystem.controller.result.WinningRecordResult;
import org.ljy.lotterysystem.service.IDrawPrizeService;
import org.ljy.lotterysystem.service.dto.WinningRecordDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class DrawPrizeController {

    @Autowired
    private IDrawPrizeService drawPrizeService;

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

//    @Autowired
//    private RabbitMessagingTemplate rabbitMessagingTemplate;

    /**
     *  这是一轮抽奖。比如说抽取某个奖品时
     * @param param
     * @return
     */
    @RequestMapping("/draw-prize")
    public CommonResult<Boolean> drawPrize(@Validated @RequestBody DrawPrizeParam param) {
        logger.info("DrawPrizeController.drawPrize:{}", JacksonUtil.writeValueAsString(param));
        drawPrizeService.drawPrize(param);
        return CommonResult.success(true);
    }

    /**
     * 展示最终抽奖结果
     * @param param
     * @return
     */
    @RequestMapping("/winning-records/show")
    public CommonResult<List<WinningRecordResult>> showWinningRecords(@Validated @RequestBody ShowWinningRecordsParam param) {
        logger.info("DrawPrizeController--showWinningRecords,{}", JacksonUtil.writeValueAsString(param));
        List<WinningRecordDTO> winningRecordDTOList = drawPrizeService.getRecords(param);
        return CommonResult.success(convertToWinningRecordResultList(winningRecordDTOList));
    }

    private List<WinningRecordResult> convertToWinningRecordResultList(List<WinningRecordDTO> winningRecordDTOList) {
        if(CollectionUtils.isEmpty(winningRecordDTOList)) {
            return Arrays.asList();
        }
        return winningRecordDTOList.stream()
                .map(winningRecordDTO->{
                    WinningRecordResult winningRecordResult = new WinningRecordResult();
                    winningRecordResult.setActivityId(winningRecordDTO.getActivityId());
                    winningRecordResult.setWinnerName(winningRecordDTO.getWinnerName());
                    winningRecordResult.setPrizeName(winningRecordDTO.getPrizeName());
                    winningRecordResult.setPrizeTier(winningRecordDTO.getPrizeTier().getMessage());
                    winningRecordResult.setWinningTime(winningRecordDTO.getWinningTime());
                    return winningRecordResult;
                })
                .collect(Collectors.toList());
    }

}
